package com.yanqu.road.server.cmd.banquet.party;

import com.yanqu.road.entity.banquet.CrossPartyRelationship;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.banquet.BanquetProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.banquet.BanquetRelationshipMgr;
import com.yanqu.road.server.pb.BanquetPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.S_BANQUET_RELATIONSHIP_GET, desc = "请求宴会互宴信息列表")
public class PartyRelationshipCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
   //     player.sendChannelPacket(CrossProtocol.C_CROSS_PARTY_RELATIONSHIP, BanquetProto.CrossPartyRelationshipReqMsg.newBuilder());
        Map<Long, CrossPartyRelationship> recordMap = BanquetRelationshipMgr.getBanquetRelationship(player.getUserId(),1,0);
        List<CrossPartyRelationship> recordList = new ArrayList<>(recordMap.values());
        recordList.sort((o1, o2) -> {
            if (o1.getTo() + o1.getFrom() == o2.getTo() + o2.getFrom()) {
                return 0;
            }
            if (o1.getTo() + o1.getFrom() >= o2.getTo() + o2.getFrom()) {
                return -1;
            }
            return 1;
        });
        if (recordList.size() > GameConfig.BANQUET_RELATIONSHIP_LIST_MAX) {
            int index = recordList.size() - 1;
            for (; index >= GameConfig.BANQUET_RELATIONSHIP_LIST_MAX; index--) {
                recordList.remove(index);
            }
        }
        BanquetProto.BanquetRelationshipRespMsg.Builder respMsg = BanquetPb.parseCrossBanquetRelationshipRespMsg(recordList);
        player.sendPacket(Protocol.U_BANQUET_RELATIONSHIP_GET, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
